package project;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class five {
    private ArrayList<Team> teams;

    public five() {
        this.teams = new ArrayList<>();
    }

    public void addTeam(String name) {
        teams.add(new Team(name));
    }

    public void updateScores(String homeName, String awayName, int homeScore, int awayScore) {
        Team homeTeam = findTeam(homeName);
        Team awayTeam = findTeam(awayName);

        if (homeTeam == null || awayTeam == null) {
            System.out.println("Error: One or both teams not found in the league.");
            return;
        }

        if (homeScore > awayScore) {
            homeTeam.addWin(homeScore, awayScore);
            awayTeam.addLoss(awayScore, homeScore);
        } else if (homeScore < awayScore) {
            homeTeam.addLoss(homeScore, awayScore);
            awayTeam.addWin(awayScore, homeScore);
        } else {
            homeTeam.addDraw(homeScore, awayScore);
            awayTeam.addDraw(awayScore, homeScore);
        }
    }

    private Team findTeam(String name) {
        for (Team team : teams) {
            if (team.getName().equals(name)) {
                return team;
            }
        }
        return null;
    }

    public void sortTeams() {
        Collections.sort(teams, new Comparator<Team>() {
            @Override
            public int compare(Team a, Team b) {
                if (a.getPoints() != b.getPoints()) return b.getPoints() - a.getPoints();
                if (a.getGoalDifference() != b.getGoalDifference()) return b.getGoalDifference() - a.getGoalDifference();
                return b.getGoalsScored() - a.getGoalsScored();
            }
        });
    }

    public void displayLeagueTable() {
        for (Team team : teams) {
            System.out.println("队名: " + team.getName() +
                               ", 比赛场数: " + team.getGamesPlayed() +
                               ", 赢: " + team.getWins() +
                               ", 平: " + team.getDraws() +
                               ", 负: " + team.getLosses() +
                               ", 进球数: " + team.getGoalsScored() +
                               ", 失球数: " + team.getGoalsConceded() +
                               ", 积分: " + team.getPoints());
        }
    }

    public static void main(String[] args) {
        five league = new five();

        // 添加一些球队到联赛
        league.addTeam("TeamA");
        league.addTeam("TeamB");
        league.addTeam("TeamC");

        // 从键盘输入比赛结果
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入比赛结果（格式：主队名 客队名 主队得分 客队得分），输入'end'结束：");
        while (true) {
            String homeName = scanner.next();
            if (homeName.equals("end")) break;
            String awayName = scanner.next();
            int homeScore = scanner.nextInt();
            int awayScore = scanner.nextInt();
            league.updateScores(homeName, awayName, homeScore, awayScore);
        }

        // 排序球队
        league.sortTeams();

        // 显示联赛表格
        league.displayLeagueTable();

        scanner.close();
    }
}

class Team {
    private String name;
    private int gamesPlayed;
    private int wins;
    private int draws;
    private int losses;
    private int goalsScored;
    private int goalsConceded;
    private int points;

    public Team(String name) {
        this.name = name;
        this.gamesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsScored = 0;
        this.goalsConceded = 0;
        this.points = 0;
    }

    public void addWin(int score, int against) {
        gamesPlayed++;
        wins++;
        goalsScored += score;
        goalsConceded += against;
        points += 3;
    }

    public void addDraw(int score, int against) {
        gamesPlayed++;
        draws++;
        goalsScored += score;
        goalsConceded += against;
        points++;
    }

    public void addLoss(int score, int against) {
        gamesPlayed++;
        losses++;
        goalsScored += score;
        goalsConceded += against;
    }

    public int getGoalDifference() {
        return goalsScored - goalsConceded;
    }

    public String getName() {
        return name;
    }

    public int getGamesPlayed() {
        return gamesPlayed;
    }

    public int getWins() {
        return wins;
    }

    public int getDraws() {
        return draws;
    }

    public int getLosses() {
        return losses;
    }

    public int getGoalsScored() {
        return goalsScored;
    }

    public int getGoalsConceded() {
        return goalsConceded;
    }

    public int getPoints() {
        return points;
    }
}